// -----------------------------
// Author: 		Dhaenens Gert 
// Creation: 	9/17/2014 9:53:33 AM				
// -----------------------------

#ifndef _LuaStaticDataMember_inl
#define _LuaStaticDataMember_inl

#include "LuaDataType.h"
#include <string>

namespace ObjectLua
{

	template<typename Class>
	BaseLuaStaticDataMember<Class>::BaseLuaStaticDataMember(const char* name, bool bReadOnly)
		: m_lpName(nullptr)
		, m_bReadOnly(bReadOnly)
		, m_lpNext(nullptr)
	{
		size_t nameLen = strnlen_s(name, 512) + 1;
		m_lpName = new char[nameLen];
		strcpy_s(m_lpName, nameLen, name);
	}
	template<typename Class>
	BaseLuaStaticDataMember<Class>::~BaseLuaStaticDataMember()
	{
		delete[] m_lpName;
		delete m_lpNext;
	}

	template<typename Class>
	bool BaseLuaStaticDataMember<Class>::isReadOnly() const
	{
		return m_bReadOnly;
	}

	template<typename Class, typename T>
	LuaStaticDataMember<Class,T>::LuaStaticDataMember(const char* name, DataMember member, bool bReadOnly)
		: BaseLuaStaticDataMember(name, bReadOnly)
		, m_Member(member)
	{
	}

	template<typename Class, typename T>
	LuaStaticDataMember<Class,T>::~LuaStaticDataMember()
	{
	}

	template<typename Class, typename T>
	void LuaStaticDataMember<Class,T>::setValue(Lua* lpLua, int idx)
	{
		if(LuaClassRegister<T>::isRegistered())
		{
			T* value = LuaDataType<T*>::get(lpLua, idx);
			*m_Member = (*value);
		}
		else
		{
			T value = LuaDataType<T>::get(lpLua, idx);
			*m_Member = value;
		}
	}
	template<typename Class, typename T>
	void LuaStaticDataMember<Class,T>::getValue(Lua* lpLua)
	{
		if(LuaClassRegister<T>::isRegistered())
		{
			T* value = &(*m_Member);
			LuaDataType<T*>::push(lpLua, value);
		}
		else
		{
			T value = *m_Member;
			LuaDataType<T>::push(lpLua, value);
		}
	}





	template<typename Class, typename T>
	LuaStaticDataMember<Class,const T>::LuaStaticDataMember(const char* name, DataMember member, bool bReadOnly)
		: BaseLuaStaticDataMember(name, bReadOnly)
		, m_Member(member)
	{
	}

	template<typename Class, typename T>
	LuaStaticDataMember<Class,const T>::~LuaStaticDataMember()
	{
	}

	template<typename Class, typename T>
	void LuaStaticDataMember<Class,const T>::setValue(Lua* lpLua, int idx)
	{
		lpLua->pushError("Trying to modify constant datamember.");
	}
	template<typename Class, typename T>
	void LuaStaticDataMember<Class,const T>::getValue(Lua* lpLua)
	{
		if(LuaClassRegister<T>::isRegistered())
		{
			const T* value = &(*m_Member);
			LuaDataType<const T*>::push(lpLua, value);
		}
		else
		{
			const T value = *m_Member;
			LuaDataType<const T>::push(lpLua, value);
		}
	}


}

#endif